<html>
<head><meta charset="utf-8"><title>edition hygiene · t-compiler · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/index.html">t-compiler</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/edition.20hygiene.html">edition hygiene</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="130625606"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/edition%20hygiene/near/130625606" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> alercah <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/edition.20hygiene.html#130625606">(Jul 31 2018 at 06:48)</a>:</h4>
<p><span class="user-mention" data-user-id="123856">@Vadim Petrochenkov</span> \o</p>



<a name="130625743"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/edition%20hygiene/near/130625743" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> alercah <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/edition.20hygiene.html#130625743">(Jul 31 2018 at 06:52)</a>:</h4>
<p>It would be nice for me to understand about hygiene in general and edition hygiene in particular. I'm especially a bit confused by your comment that edition hygiene isn't part of the language.</p>



<a name="130630259"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/edition%20hygiene/near/130630259" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Vadim Petrochenkov <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/edition.20hygiene.html#130630259">(Jul 31 2018 at 08:59)</a>:</h4>
<p>The general idea is pretty simple - each token is produced by some specific macro, each macro is defined by some crate, that crate is built with some <code>--edition</code> option and value of that option is kept in crate metadata.<br>
So, ultimately, we know edition for each token.</p>



<a name="130630395"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/edition%20hygiene/near/130630395" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Vadim Petrochenkov <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/edition.20hygiene.html#130630395">(Jul 31 2018 at 09:02)</a>:</h4>
<p>When we do something with that token (parse it, or perform some semantic checks later), we can extract its edition (from its span, all hygiene data is kept through spans) and act differently depending on edition value.</p>



<a name="130630858"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/edition%20hygiene/near/130630858" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Vadim Petrochenkov <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/edition.20hygiene.html#130630858">(Jul 31 2018 at 09:13)</a>:</h4>
<blockquote>
<p>I'm especially a bit confused by your comment that edition hygiene isn't part of the language</p>
</blockquote>
<p>I mean, C++17 is a language, C++11 is a language, Rust 2015 is a language, Rust 2018 is a language, but any attempts to build one library in one language, another library in another language and combine them together is already in the tooling territory.</p>



<a name="130653063"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/edition%20hygiene/near/130653063" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> alercah <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/edition.20hygiene.html#130653063">(Jul 31 2018 at 16:09)</a>:</h4>
<p>Right, ok, that makes sense. So that is used for things like ensuring that if I pass a new keyword from a previous edition into a new one, it stays a raw identifier and doesn't get reinterpreted as a keyword, right?</p>



<a name="130653191"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/edition%20hygiene/near/130653191" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> alercah <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/edition.20hygiene.html#130653191">(Jul 31 2018 at 16:11)</a>:</h4>
<p>And I saw the issue about the complexity of handling it for expressions, etc.</p>



<a name="130653278"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/edition%20hygiene/near/130653278" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> alercah <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/edition.20hygiene.html#130653278">(Jul 31 2018 at 16:12)</a>:</h4>
<p>The one where you can have an incomplete expression returned from a macro really got me. Is it possible that that's an issue we can/should punt on for now, and just let us decide hygiene for other things as we introduce features that require it?</p>



<a name="130653324"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/edition%20hygiene/near/130653324" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> alercah <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/edition.20hygiene.html#130653324">(Jul 31 2018 at 16:13)</a>:</h4>
<p>like saying "ok, so edition hygiene affects identifiers for name lookup and keyword purposes, but we'll leave everything else unspecified for now"? Is there any other cases where hygiene would come up?</p>



<a name="130653795"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/edition%20hygiene/near/130653795" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> alercah <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/edition.20hygiene.html#130653795">(Jul 31 2018 at 16:20)</a>:</h4>
<p>err, edition hygiene</p>



<a name="130653888"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/edition%20hygiene/near/130653888" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> alercah <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/edition.20hygiene.html#130653888">(Jul 31 2018 at 16:22)</a>:</h4>
<p>I know that for hygiene purposes, we have at the very least to deal with name lookup and visibility. I don't fully understand all the options for controlling hygiene, though.</p>



<a name="130654148"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/edition%20hygiene/near/130654148" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> alercah <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/edition.20hygiene.html#130654148">(Jul 31 2018 at 16:26)</a>:</h4>
<p>I know we have <code>$crate</code> which forces a name to be looked up locally, <code>local_inner_macros</code> which does that for all macros in a macro definition, right? And <code>call_site</code> is the only stable option for proc macros, but we want to let you have definition-site hygiene if you opt to? Can you declare items in definition-site hygiene? Is the behaviour of <code>quote</code> (and the crates with similar functionality) with respect to hygiene understood/defined? Does <code>TokenStream::parse()</code> always use call-site?</p>



<a name="130669728"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/edition%20hygiene/near/130669728" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Vadim Petrochenkov <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/edition.20hygiene.html#130669728">(Jul 31 2018 at 20:26)</a>:</h4>
<blockquote>
<p>So that is used for things like ensuring that if I pass a new keyword from a previous edition into a new one, it stays a raw identifier and doesn't get reinterpreted as a keyword, right?</p>
</blockquote>
<p>Right.<br>
(For keywords this is simple since they are single tokens.)</p>
<blockquote>
<p>Is it possible that that's an issue we can/should punt on for now, and just let us decide hygiene for other things as we introduce features that require it?</p>
</blockquote>
<p>I think we can do that in practice.<br>
The absolute path issue is probably the only issue here that's urgent or important, since the module reform both 1) focuses everyone's attention on path starts, and 2) makes incompatible changes in that area requiring compatibility between edition.</p>
<blockquote>
<p>I know we have $crate which forces a name to be looked up locally, local_inner_macros which does that for all macros in a macro definition, right?</p>
</blockquote>
<p>Right.<br>
(Locally means in the root of the current crate, where "current" is the crate that defined the macro... in some sense, see examples with nested macro definitions in <a href="https://github.com/rust-lang/rust/pull/51762/commits/b69d51162b5391119e86d1c6e884aa09292a7806" target="_blank" title="https://github.com/rust-lang/rust/pull/51762/commits/b69d51162b5391119e86d1c6e884aa09292a7806">https://github.com/rust-lang/rust/pull/51762/commits/b69d51162b5391119e86d1c6e884aa09292a7806</a>).</p>
<blockquote>
<p>And call_site is the only stable option for proc macros, but we want to let you have definition-site hygiene if you opt to?</p>
</blockquote>
<p>Yes.</p>
<blockquote>
<p>Can you declare items in definition-site hygiene?</p>
</blockquote>
<p>Yes, they will be local to the macro and effectively inaccessible from the outside, perfectly suits for some internal helper stuff.</p>
<blockquote>
<p>Is the behaviour of quote (and the crates with similar functionality) with respect to hygiene understood/defined?</p>
</blockquote>
<p>Builtin <code>quote</code> macro generates tokens with def-site hygiene.<br>
Third-party crates working on stable generate tokens with call-site hygiene, since only call-site hygiene is stable.<br>
So its behavior is well-defined in terms of these two hygiene kinds.</p>
<blockquote>
<p>Does TokenStream::parse() always use call-site?</p>
</blockquote>
<p>Yes.</p>



<a name="130679606"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/edition%20hygiene/near/130679606" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> alercah <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/edition.20hygiene.html#130679606">(Aug 01 2018 at 00:02)</a>:</h4>
<p>Thanks!</p>
<p>Bit more: in paths or field/method access, how is visibility handled? Is it always by simply looking at the first element in the path/access expression and seeing where it is? How does this relate to a method of a trait? e.g. in <code>a.b()</code>, where does <code>b</code> need to be in scope? at the site of <code>a</code> or the site of <code>b</code>?</p>
<p>If you can elaborate as much as possible here, so that I can someday write this into the reference, I would appreciate that. :)</p>



<a name="130680838"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/edition%20hygiene/near/130680838" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> alercah <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/edition.20hygiene.html#130680838">(Aug 01 2018 at 00:37)</a>:</h4>
<p>There was a discussion in Discord that led to an interesting case. There is currently an RFC proposing private impls of foreign types (for traits at the moment, but several people have gone "ah but what if we could expand this to inherent impls too?"). If that happened, then what would the following do?</p>
<div class="codehilite"><pre><span></span><span class="k">impl</span><span class="w"> </span><span class="nb">String</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">  </span><span class="k">fn</span> <span class="nf">len</span><span class="p">(</span><span class="o">&amp;</span><span class="bp">self</span><span class="p">)</span><span class="w"> </span>-&gt; <span class="kt">usize</span> <span class="p">{</span><span class="w"> </span><span class="mi">0</span><span class="w"> </span><span class="p">}</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>

<span class="k">fn</span> <span class="nf">unhygienic_maybe</span><span class="p">()</span><span class="w"> </span>-&gt; <span class="kt">usize</span> <span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="kd">let</span><span class="w"> </span><span class="n">s</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s">&quot;string&quot;</span><span class="p">.</span><span class="n">to_string</span><span class="p">();</span><span class="w"></span>
<span class="w">    </span><span class="n">imported_macro</span><span class="o">!</span><span class="p">(</span><span class="n">s</span><span class="p">)</span><span class="w"> </span><span class="c1">// imported_macro! expands to &quot;s.len()&quot;, with def-site hygiene on &#39;len&#39;</span>
<span class="p">}</span><span class="w"></span>
</pre></div>


<p>Would this call the normal <code>String::len</code> or the one declared locally?</p>
<p>(Side note that I just want to get down: perhaps <code>quote</code> should have a way to explicitly invoke call-site hygiene, e.g. by writing <code>#a</code>?)</p>



<a name="130703852"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/edition%20hygiene/near/130703852" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Vadim Petrochenkov <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/edition.20hygiene.html#130703852">(Aug 01 2018 at 10:01)</a>:</h4>
<blockquote>
<p>in paths or field/method access, how is visibility handled?</p>
</blockquote>
<p>Visibility as in privacy, or in the hygiene meaning? (I assume the second.)</p>
<blockquote>
<p>Is it always by simply looking at the first element in the path/access expression and seeing where it is?</p>
</blockquote>
<p>Not sure what this means exactly, but it's close to what happens.</p>
<blockquote>
<p>e.g. in a.b(), where does b need to be in scope? at the site of a or the site of b?</p>
</blockquote>
<p>The site of <code>b</code>.<br>
Name resolution hygiene is identifier-based, we see and identifier <code>b</code>, we resolve it from it from its def-site, <code>a</code> is not relevant here (well, it may be relevant for collecting candidates, but not for finding the correct <code>b</code> among them).</p>
<p>The best intuition about def-site hygiene is probably replacing the macro with and inline function and then resolving names from that function as you would usually do that without any macros.</p>



<a name="130704051"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/edition%20hygiene/near/130704051" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Vadim Petrochenkov <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/edition.20hygiene.html#130704051">(Aug 01 2018 at 10:07)</a>:</h4>
<blockquote>
<p>Would this call the normal String::len or the one declared locally?</p>
</blockquote>
<p>Let's assume <code>imported_macro</code> is a function and not a macro, then what <code>len</code> would be in scope in it? Certainly not the locally defined one. Hygienic macro behaves in the same way.</p>
<blockquote>
<p>perhaps quote should have a way to explicitly invoke call-site hygiene, e.g. by writing #a?</p>
</blockquote>
<p>Yes, perhaps, similarly to <a href="https://github.com/rust-lang/rfcs/pull/2498" target="_blank" title="https://github.com/rust-lang/rfcs/pull/2498">https://github.com/rust-lang/rfcs/pull/2498</a>. Hygienic macros do need some kind of opt-out to do anything practical.</p>



<a name="130981555"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/edition%20hygiene/near/130981555" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/edition.20hygiene.html#130981555">(Aug 06 2018 at 14:25)</a>:</h4>
<p><span class="user-mention" data-user-id="123856">@Vadim Petrochenkov</span> we still haven't made any kind of "formal decision" about edition hygiene, and the various models, right? I still lean towards the "take edition from some significant token variant", in part because it seems like something we can readily explain and document, but it seems like we need someone to take charge there and try to write out all the rules and get feedback on them.</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>